package com.morn.fanfou.widget;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.morn.fanfou.ConfigActivity;
import com.morn.fanfou.FanfouApplication;
import com.morn.fanfou.R;
import com.morn.fanfou.api.Fanfou;
import com.morn.fanfou.http.ImageTool;
import com.morn.fanfou.model.Conversation;
import com.morn.fanfou.model.Message;
import com.morn.fanfou.model.User;
import com.morn.fanfou.util.DateUtil;

public class ConversationListAdapter extends BaseAdapter {

	private List<Conversation> list;
	private LayoutInflater inflater;
	private static int resource = R.layout.conversation_list_item;
	private ImageTool asyncImageDownloader;

	private boolean isShowUserPhoto;
	private Context context;

	public ConversationListAdapter(Context context, List<Conversation> list) {
		this.context = context;
		this.list = list;
		this.inflater = LayoutInflater.from(context);
		Activity d = (Activity) context;
		Fanfou fanfou = (((FanfouApplication) d.getApplication()).getFanfou());
//		asyncImageDownloader = new ImageTool(fanfou.getHttpClient(), context);
		isShowUserPhoto = ConfigActivity.isShowUserPhoto(context);

	}

	@Override
	public void notifyDataSetChanged() {
		isShowUserPhoto = ConfigActivity.isShowUserPhoto(context);
		super.notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Conversation conversation = list.get(position);
		Message message = conversation.getMessage();
		View vi = convertView;
		ViewHolder holder;
		if (convertView == null) {
			vi = inflater.inflate(resource, null);
			holder = new ViewHolder();
			holder.photo = (ImageView) vi
					.findViewById(R.id.conversation_item_photo);
			holder.name = (TextView) vi
					.findViewById(R.id.conversation_item_name);
			holder.time = (TextView) vi
					.findViewById(R.id.conversation_item_time);
			holder.msg = (TextView) vi.findViewById(R.id.conversation_item_msg);
			holder.count = (TextView) vi
					.findViewById(R.id.conversation_item_count);
			vi.setTag(holder);
		} else
			holder = (ViewHolder) vi.getTag();

		holder.time.setText(DateUtil.getDateStr(message.getCreated_at()));
		holder.msg.setText(message.getText());
		holder.count.setText(conversation.getMsg_num()+"");

		String otherId = conversation.getOtherid();

		User user1 = message.getSender();
		User user2 = message.getRecipient();
		User otherUser = null;
		if (otherId.equals(user1.getId())) {
			otherUser = user1;
		} else {
			otherUser = user2;
		}
		holder.name.setText(otherUser.getName());
		String imageUrl = otherUser.getProfile_image_url();

		if (isShowUserPhoto) {
			holder.photo.setVisibility(View.VISIBLE);
			if (imageUrl != null) {
				asyncImageDownloader.download(imageUrl, holder.photo);
			}
		} else {
			holder.photo.setVisibility(View.GONE);
		}

		return vi;
	}

	public static class ViewHolder {
		public ImageView photo;
		public TextView name;
		public TextView time;
		public TextView msg;
		public TextView count;
	}

}
